//
//  YBMineFpsPopupVC.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/11/1.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit

class YBMineFpsPopupVC: BaseMaskVC {
    
    enum FpsType: Int, CaseIterable {
        case fps30 = 0
        case fps60
        
        var des: String {
            switch self {
            case .fps30: return "30 FPS"
            case .fps60: return "60 FPS"
            }
        }
    }
    
    private var selectedIndex: Int = 0 {
        didSet {
            for case let item as YBMineFpsItemView in stackView.arrangedSubviews {
                item.isSelect = (item.tag == selectedIndex)
            }
        }
    }
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        FpsType.allCases.forEach { type in
            let item = YBMineFpsItemView()
            item.tag = type.rawValue
            item.itemLabel.text = type.des
            item.whenTap { [unowned self] tap in
                self.selectedIndex = item.tag
            }
            stackView.addArrangedSubview(item)
        }
        
        view.addSubview(bgView)
        bgView.addSubviews([titleLabel, stackView, cancelBtn])
        
        bgView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(kScreenWidth*0.84)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.top.equalTo(20).priority(.high)
            make.height.equalTo(24)
        }
        
        stackView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.left.width.right.equalToSuperview()
            make.height.equalTo(44*CGFloat(FpsType.allCases.count))
        }
        
        cancelBtn.snp.makeConstraints { make in
            make.top.equalTo(stackView.snp.bottom).offset(16)
            make.right.equalTo(-16)
            make.bottom.equalTo(-16).priority(.high)
        }
        
    }
    
    // MARK: - lazy
    private lazy var bgView: UIView = {
        let view = UIView(color: .lightWhiteDark27)
        view.bk_addStyleWith(cornerRadius: 8, corners: .allCorners)
        return view
    }()
    
    private lazy var titleLabel = self.bk_addLabel(text: "FPS", font: .systemFont(ofSize: 20, weight: .medium), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
    private lazy var stackView: UIStackView = {
        let _stackView = UIStackView()
        _stackView.axis = .vertical
        _stackView.spacing = 0
        _stackView.alignment = .fill
        _stackView.distribution = .fillEqually
        return _stackView
    }()
    
    private lazy var cancelBtn: UIButton = {
        let btn = self.bk_addButton(title: "取消", font: .systemFont(ofSize: 15), bgColor: .clear, titleColor: .blue)
        btn.bk_addTarget { [weak self] sender in
            self?.dismiss(animated: true)
        }
        return btn
    }()
    
}

// MARK: - YBMineFpsItemView

class YBMineFpsItemView: UIView {
    
    var isSelect: Bool = false {
        didSet {
            checkRoundBtn.isSelected = isSelect
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubviews([checkRoundBtn, itemLabel])
        checkRoundBtn.snp.makeConstraints { make in
            make.size.equalTo(24)
            make.left.equalTo(18)
            make.centerY.equalToSuperview()
        }
        
        itemLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(checkRoundBtn.snp.right).offset(24)
            make.right.equalTo(-16)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var checkRoundBtn: UIButton = {
        let btn = self.bk_addImgSelectButton(iconNormal: "icon_check_greenround_normal", iconSelect: "icon_check_greenround_select")
        btn.isUserInteractionEnabled = false
        return btn
    }()
    
    lazy var itemLabel = self.bk_addLabel(font: .systemFont(ofSize: 15), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
}
